/*==============================================================================
*	File Name                 Bsp_ADC.c
*	Functional Description    ADC function Control
*	Other					  be used to collect voltage
*   Date                      2025/11/10
*	Author                    LIAI.
==============================================================================*/
#include "Bsp_ADC.h"

//------------------------------------------------------------------------------
//	macro define （宏定义）
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//	function declaration（函数声明）
//------------------------------------------------------------------------------
void Bsp_ADC1Init(void);
uint16_t Bsp_ADCGetVolt(void);
uint16_t Bsp_ADCGetValue(void);
uint16_t Bsp_Meanfilter(uint16_t *buffer, uint8_t count);
//------------------------------------------------------------------------------
//	para define（变量定义）
//------------------------------------------------------------------------------


//------------------------------------------------------------------------------
//	function define （函数定义）
//------------------------------------------------------------------------------
/*------------------------------------------------------------------------------
*   @name    	Bsp_ADC1Init
*   @brief		ADC1  初始化
*   @param[in]	void
*   @return		void
*   @attention	底层GPIO、ADC初始化
*               PD6->ADC_IN6/单次转换/4Mhz/GPIO触发/右对齐
------------------------------------------------------------------------------*/
void Bsp_ADC1Init(void)
{

  GPIO_Init(ADC1_POWER_PORT, ADC1_POWER_PIN, GPIO_MODE_IN_FL_NO_IT);

  ADC1_DeInit();
  ADC1_Init(ADC1_CONVERSIONMODE_SINGLE, ADC1_POWER_CHANNEL, ADC1_PRESSEL_FCPU_D4,
            ADC1_EXTTRIG_GPIO, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL6, DISABLE);

  ADC1_Cmd(ENABLE);


}

/*------------------------------------------------------------------------------
*   @name    	Bsp_ADCGetVolt
*   @brief		获取ADC实际转化电压值
*   @param[in]	void
*   @return		uint16_t
*   @attention	ADC:10 BIT->1024;  adc_volt：放大100倍
------------------------------------------------------------------------------*/
uint16_t Bsp_ADCGetVolt(void)
{
  uint16_t adc_volt = (Bsp_ADCGetValue() * ADC1_TRANSFRE_VALUE); //放大100倍
  return adc_volt;
}

/*------------------------------------------------------------------------------
*   @name    	Bsp_GetValue
*   @brief		获取ADC转换值
*   @param[in]	void
*   @return		void
*   @attention	取8个数值之和得到6组数据
------------------------------------------------------------------------------*/
uint16_t Bsp_ADCGetValue(void)
{
  uint8_t count,i,j;
  uint16_t adc_min = 0;
  uint16_t adc_max = 0;
  uint16_t adc_value[10] = {0};
  uint16_t VoltCNT[10] = {0};

  for(count = 0; count <6 ; count++)
  {
    for(i = 0; i < 10; i++)
    {
      ADC1_StartConversion();
      adc_value[i] = ADC1_GetConversionValue();
    }
    adc_min = adc_value[0];
    adc_max = adc_value[0];
    for(j = 0; j < 10; j++)
    {
       if(adc_value[j] > adc_max)
       {
         adc_max = adc_value[j];
       }
       if(adc_value[j] < adc_min)
       {
         adc_min = adc_value[j];
       }
       VoltCNT[count] += adc_value[j];
       VoltCNT[count] = (u16)(VoltCNT[count]- adc_max-adc_min);
    }
  }
  return Bsp_Meanfilter(VoltCNT, 6);
}

/*------------------------------------------------------------------------------
*   @name    	Bsp_Meanfilter
*   @brief		冒泡排序
*   @param[in]	*buffer 参数首地址
*               count 参数个数
*   @return		uint16_t 参数中间值
------------------------------------------------------------------------------*/
uint16_t Bsp_Meanfilter(uint16_t *buffer, uint8_t count)
{
  uint8_t k, l;
  uint16_t final_value = 0;
  for(k = 0; k < count; k++)
  {
     for(l = 0; l < (count-k); l++)
     {
       if(buffer[l] > buffer[l+1])
       {
         uint16_t  temp = buffer[l];
         buffer[l] = buffer[l+1];
         buffer[l+1] = temp;
       }
     }
  }
    if(count % 2 == 0)
  {
    // 偶数
    final_value = (buffer[count/2 - 1] + buffer[count/2]) / 2;
  }
  else
  {
    // 奇数
    final_value = buffer[count/2];
  }

  return final_value ;
}

